package cn.mdmm.controller.system;

import java.io.PrintWriter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import cn.mdmm.common.MessageConstant;
import cn.mdmm.common.SpringSecurityUtils;
import cn.mdmm.controller.BaseController;
import cn.mdmm.entity.response.ResponseBodyToOne;
import cn.mdmm.entity.system.MenuTree;
import cn.mdmm.entity.system.SysRole;
import cn.mdmm.entity.system.SysRoleMenu;
import cn.mdmm.entity.system.SysUser;
import cn.mdmm.service.system.SysDictService;
import cn.mdmm.service.system.SysMenuService;
import cn.mdmm.service.system.SysRoleService;
import cn.modoumama.common.RequestUtils;
import cn.modoumama.common.annotation.OpLog;
import cn.modoumama.common.exception.ServiceException;
import cn.modoumama.common.exception.SystemException;
import cn.modoumama.common.utils.JSONUtils;
import cn.modoumama.page.GridDataModel;
import cn.modoumama.page.Page;

@Controller
@RequestMapping(value = "/system/prg/role")
@OpLog(logDesc = "角色控制")
public class SysRoleController extends BaseController {
	@Autowired
	private SysRoleService sysRoleService;
	@Autowired
	private SysMenuService sysMenuService;
	@Autowired
	private SysDictService sysDictService;

	@RequestMapping(value = "init")
	public String init(HttpServletRequest request) {
		Map<String, String> statusMap = sysDictService.getDetailValueMap("状态");
		request.setAttribute("statusMap", JSONUtils.toJson(statusMap));
		request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		SysUser sysUser = SpringSecurityUtils.getCurrentUser();
		Map<String, String> typeMap = sysDictService.getDetailValueMap("用户类型", sysUser.getUserType());
		request.setAttribute("typeMap", JSONUtils.toJson(typeMap));
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeMap));
		return "system/role/init";
	}

	@RequestMapping(value = "list")
	@ResponseBody
	public GridDataModel list(HttpServletRequest request, HttpServletResponse response, SysUser user) {
		logger.debug("method: list() ");
		Page po = RequestUtils.getPage(request, "ROLE_ORDER asc");
		po.getCondition().put("roleNameLike", request.getParameter("roleName"));
		po.getCondition().put("createTimeStart", request.getParameter("createTimeStart"));
		po.getCondition().put("createTimeEnd", request.getParameter("createTimeEnd"));
		po.getCondition().put("updateTimeStart", request.getParameter("updateTimeStart"));
		po.getCondition().put("updateTimeEnd", request.getParameter("updateTimeEnd"));
		GridDataModel gridDataModel = sysRoleService.findGridDataModelByCondition(po);
		return gridDataModel;
	}

	@RequestMapping(value = "showAdd")
	public String showAdd(HttpServletRequest request) {
		Map<String, String> statusMap = sysDictService.getDetailValueMap("状态");
		request.setAttribute("statusMap", JSONUtils.toJson(statusMap));
		request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		SysUser sysUser = SpringSecurityUtils.getCurrentUser();
		Map<String, String> typeMap = sysDictService.getDetailValueMap("用户类型", sysUser.getUserType());
		request.setAttribute("typeMap", JSONUtils.toJson(typeMap));
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeMap));
		return "system/role/add";
	}

	@RequestMapping(value = "insert")
	public String insert(HttpServletRequest request, SysRole sRole) {
		sRole.setCreateTime(new Date());
		if (sRole.getRoleType() == null || sRole.getRoleType() <= SpringSecurityUtils.getCurrentUserType()) {
			sRole.setRoleType(SpringSecurityUtils.getCurrentUserType() + 1);
		}
		sysRoleService.insert(sRole);
		return "success";
	}

	@RequestMapping(value = "showEdit")
	public String showEdit(HttpServletRequest request) {
		Long roleId = Long.valueOf(request.getParameter("roleId"));
		SysRole sRole = sysRoleService.findById(roleId);
		request.setAttribute("sRole", sRole);
		Map<String, String> statusMap = sysDictService.getDetailValueMap("状态");
		request.setAttribute("statusCombo", JSONUtils.toCombo(statusMap));
		SysUser sysUser = SpringSecurityUtils.getCurrentUser();
		Map<String, String> typeMap = sysDictService.getDetailValueMap("用户类型", sysUser.getUserType());
		request.setAttribute("typeMap", JSONUtils.toJson(typeMap));
		request.setAttribute("typeCombo", JSONUtils.toCombo(typeMap));
		return "system/role/edit";
	}

	@RequestMapping(value = "update")
	public String update(HttpServletRequest request, SysRole sRole) {
		sRole.setUpdateTime(new Date());
		if (sRole.getRoleType() == null || sRole.getRoleType() <= SpringSecurityUtils.getCurrentUserType()) {
			sRole.setRoleType(SpringSecurityUtils.getCurrentUserType() + 1);
		}
		sysRoleService.mergeById(sRole);
		return "success";
	}

	@RequestMapping(value = "delete")
	@ResponseBody
	public ResponseBodyToOne delete(HttpServletRequest request, PrintWriter out) {
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		logger.debug("method: delete() ");
		try {
			String ids = request.getParameter("ids");
			String[] roleIds = ids.split(",");
			sysRoleService.deleteRoleByIds(roleIds);
		} catch (Exception e) {
			throw new ServiceException(MessageConstant.SYSTEM_ERROR);
		}
		return responseBody;
	}

	@RequestMapping(value = "checkRole")
	@ResponseBody
	public ResponseBodyToOne checkRoleName(HttpServletRequest request, HttpServletResponse response, SysRole role) {
		ResponseBodyToOne responseBody = new ResponseBodyToOne();
		Map<String, Object> condititon = new HashMap<String, Object>();
		condititon.put("roleName", role.getRoleName());
		condititon.put("roleEnglishName", role.getRoleEnglishName());
		condititon.put("notRoleId", role.getRoleId());
		int flag = sysRoleService.countByCondition(condititon);
		if (flag > 0) {
			throw new SystemException("不存在");
		}
		return responseBody;
	}

	@RequestMapping(value = "menuTree")
	public String menuTree(HttpServletRequest request) {
		Long roleId = Long.valueOf(request.getParameter("roleId"));
		List<SysRoleMenu> SRoleMenu = sysRoleService.selectRoleMenuByCondition(roleId);
		request.setAttribute("roleId", roleId);
		request.setAttribute("SRoleMenuJson", JSONUtils.toJson(SRoleMenu));
		List<MenuTree> menuTrees = sysMenuService.listTree(true, false, false);
		request.setAttribute("resourceTree", JSON.toJSON(menuTrees));
		return "system/role/menuTree";
	}

	@RequestMapping(value = "bindMenu")
	public String bindMenu(HttpServletRequest request) {
		Long roleId = Long.valueOf(request.getParameter("roleId"));
		String menuIds = request.getParameter("menuIds");
		if (menuIds != null && menuIds != "") {
			String aMenuIds[] = menuIds.split(",");
			sysRoleService.bindRoleMenu(roleId, aMenuIds);
			SpringSecurityUtils.clearAllCachedAuthorizationInfo();
		}
		return "success";
	}

}
